//
//  CRMCustomerDetailVC.m
//  New_Runway
//
//  Created by NewLife on 2018/2/26.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CRMCustomerDetailVC.h"
#import "CustomerDetailCell.h"
#import "CRMCustomerHeaderView.h"
#import "NRWCarBottomMenuView.h"

#import "CRMEditorCustomerVC.h"
#import "NRWCarAddIntentionVC.h"
#import "NRWCarCustomerIntentModel.h"
#import "CRMFollowUpListVC.h"
#import "CRMFollowHistoryVC.h"

@interface CRMCustomerDetailVC ()<UITableViewDelegate, UITableViewDataSource, NRWCarBottomMenuViewDelegate, CRMCustomerHeaderViewDelegate>

@property (strong, nonatomic) NRWCarCustomerIntentModel *model;

@end

@implementation CRMCustomerDetailVC
{
    UITableView *_tableView;
    CRMCustomerHeaderView *header;
    NRWCarBottomMenuView *menuView;
}

#pragma mark - lifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBarView.titleLabel.text = self.customerName;
    self.view.backgroundColor = [UIColor whiteColor];
    if (self.isDataPush == YES) {
        self.navigationBarView.rightItem.hidden = YES;
    }else{
        [self.navigationBarView setupRightItemImage:[UIImage imageNamed:@"Point"]];
    }
    
    [self setupUI];
}

- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    [self loadData];
}

#pragma mark - setupUI
- (void)setupUI {
    
    _tableView = [[UITableView alloc] init];
    _tableView.frame = CGRectMake(0, self.navH, Screen_width, Screen_height - self.navH);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.opaque = YES;
    _tableView.backgroundColor = [UIColor colorFromHexCode:@"#ffffff"];
    [_tableView registerClass:[CustomerDetailCell class] forCellReuseIdentifier:@"cell"];
    _tableView.tableFooterView = [UIView new];
    [self.view addSubview:_tableView];
    
    header = [[CRMCustomerHeaderView alloc] initWithFrame:CGRectMake(0, 0, Screen_width, getRealWidth(185))];
    _tableView.tableHeaderView = header;
    header.delegate = self;
    
    [self setupMenuView];
}

- (void)setupMenuView {
    
    NSArray *title = nil;
    NSArray *image = nil;
    
    if (self.followState == 0) {
        
        title = @[@"编辑客户", @"添加意向"];
        image = @[@"crm_customeredit", @"crm_addIntention"];
    } else if (self.followState == 30 || self.followState == 40 || self.followState == 50) {
        
        title = @[@"编辑客户", @"添加意向", @"跟进历史"];
        image = @[@"crm_customeredit", @"crm_addIntention", @"crm_followHistory"];
    } else {
        
        title = @[@"编辑客户", @"添加意向", @"跟进", @"跟进历史"];
        image = @[@"crm_customeredit", @"crm_addIntention", @"crm_addIntention-1", @"crm_followHistory"];
    }
    
    menuView = [[NRWCarBottomMenuView alloc] initWithTitleArr:title imageArr:image];
    menuView.delegate = self;
}

#pragma mark - privateMethod
- (void)loadData {
    
    if (self.customerShopId.length < 1) {
        return;
    }
    
    [SVProgressHUD show];
    
    [YQNetworking postWithUrl:[NSString stringWithFormat:@"%@%@", CRMContextApi, customerDetailAPI] refreshRequest:YES cache:NO params:@{@"customerShopId": self.customerShopId} progressBlock:nil successBlock:^(id response) {
        
        [SVProgressHUD dismiss];
        
        self.model = [NRWCarCustomerIntentModel mj_objectWithKeyValues:response];
        
        if (self.model.code == 1) {//成功
            
            header.model = self.model;
            
            [_tableView reloadData];
            
        } else {//error
            
        }
        
    } failBlock:^(NSError *error) {
        [SVProgressHUD dismiss];
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];
}

- (void)rightItemClick {
    
    [menuView show];
}

#pragma mark - publicMethod

#pragma mark - DelegateMethod
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.model.result.modelList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CustomerDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    NRWCarCustomerIntentlListModel *listModel = self.model.result.modelList[indexPath.row];
    
    cell.imageUrlStr = listModel.carImageUrl;
    
    cell.carNameStr = listModel.carTitle;
    
    cell.carPriceStr = [NSString stringWithFormat:@"¥%.2f万", listModel.carRetailPrice];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 80;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor colorFromHexCode:@"f5f5f5"];
    view.opaque = YES;
    
    CALayer *layer = [CALayer layer];
    layer.backgroundColor = [UIColor colorFromHexCode:@"ff6816"].CGColor;
    layer.frame = CGRectMake(0, 0, 2, 25);
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"买车意向";
    label.textColor = [UIColor colorFromHexCode:@"323232"];
    label.font = kPingFangRegular13();
    label.frame = CGRectMake(8, 0, 100, 25);
    
    [view addSubview:label];
    [view.layer addSublayer:layer];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 25;
}

#pragma mark customDelegate
- (void)bottomMenuView:(NRWCarBottomMenuView *)view didClickBtn:(UIButton *)button {
    
    if ([button.titleLabel.text isEqualToString:@"编辑客户"]) {
        
        CRMEditorCustomerVC *vc = [[CRMEditorCustomerVC alloc] init];
        vc.customerShopId = self.customerShopId;
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([button.titleLabel.text isEqualToString:@"添加意向"]) {
        
        NRWCarAddIntentionVC *vc = [NRWCarAddIntentionVC new];
        vc.customerShopId = self.model.result.customerShopId;
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([button.titleLabel.text isEqualToString:@"跟进"]) {
        
        CRMFollowUpListVC *vc = [[CRMFollowUpListVC alloc] init];
        vc.customerShopId = self.model.result.customerShopId;
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([button.titleLabel.text isEqualToString:@"跟进历史"]) {
        
        CRMFollowHistoryVC *vc = [[CRMFollowHistoryVC alloc] init];
        vc.customerShopId = self.model.result.customerShopId;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    [view dismiss];
}

- (void)customerHeaderView:(CRMCustomerHeaderView *)view callBtnDidClick:(UIButton *)sender {
    
    [self callPhoneWithPhoneNum:self.model.result.customerPhone];
    
}

#pragma mark - touchEventAction

#pragma mark - setter

#pragma mark - getter


@end
